from typing import TypeVar, Callable, Any
from functools import wraps
from fastapi import status
from .response_json import message_json
from .basemodel import CustomResponse, CustomMessageType

T = TypeVar("T")


def custom_response_fc(data: T = None) -> CustomResponse:
    response_dict = {
        'code': status.HTTP_200_OK,
        'message': {
            'type': CustomMessageType.normal,
            'code': 'M0001',
            'desc': message_json['M0001']
        },
        'data': data
    }
    return CustomResponse(**response_dict)


def custom_response(endpoint_func: Callable[..., Any]):
    """
    定义一个装饰器来自动处理响应
    """

    @wraps(endpoint_func)
    async def wrapper(*args, **kwargs):
        response_data = await endpoint_func(*args, **kwargs)

        return custom_response_fc(data=response_data)

    return wrapper
